package com.tuyue.police.api;

import com.tuyue.police.bean.UrlBean;
import com.tuyue.police.enums.ResultEnum;
import com.tuyue.police.exception.MyException;
import com.tuyue.police.response.CodeImageResponse;
import com.tuyue.police.util.HttpUtils;
import com.tuyue.police.util.JsonToObject;
import com.tuyue.police.util.Result;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by dell on 2018/6/1.
 */

@Component
public class DeviceApi {

    @Autowired
    private DeviceApi deviceApi;

    @Autowired
    private AccessTokenApi accessTokenApi;

    /**
     * @Author: 文婧瑶
     * @Description: 添加设备
     * @Date: 14:13 2018/6/1
     */
    public Boolean addDevice(String deviceSerial, String validateCode) throws Exception {

        String accessToken = accessTokenApi.getAccessToken();
        Map<String, String> querys = new HashMap<>();
        querys.put("accessToken", accessToken);
        querys.put("deviceSerial", deviceSerial);
        querys.put("validateCode", validateCode);
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        HttpResponse response = HttpUtils.doPost(UrlBean.ADDDEVICE, querys, null);
        System.out.println("==================" + response.toString());
        String s = EntityUtils.toString(response.getEntity());
        Result result = JsonToObject.parseObject(s);
        int code = result.getCode();
        if (code == 200) {
            return true;
        } else {
            throw new MyException(code, result.getMsg());
        }

    }

    /**
     * @Author: 文婧瑶
     * @Description: 增加子账户权限
     * @Date: 15:01 2018/6/1
     */
    public Boolean setAccess(String accountId, String deviceSerial) {

        String accessToken = accessTokenApi.getAccessToken();
        String statement = "{\"Permission\": \"GET,UPDATE,DEVCTRL\", \"Resource\": [\"dev:" + deviceSerial + "\"]}";
        System.out.println(statement);
        Map<String, String> querys = new HashMap<>();
        querys.put("accessToken", accessToken);
        querys.put("statement", statement);
        querys.put("accountId", accountId);
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        HttpResponse response;
        try {
            response = HttpUtils.doPost(UrlBean.SETACCESS, querys, null);
            System.out.println("==================" + response.toString());
            String s = EntityUtils.toString(response.getEntity());
            int code = JsonToObject.parseObject(s).getCode();
            if (code == 200) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            throw new MyException(ResultEnum.YS_SERVER_ERROR);
        }
    }

    /**
     * @Author: 文婧瑶
     * @Description: 删除设备
     * @Date: 15:45 2018/6/1
     */
    public Boolean delDevice(String deviceSerial) {

        String accessToken = accessTokenApi.getAccessToken();
        Map<String, String> querys = new HashMap<>();
        querys.put("accessToken", accessToken);
        querys.put("deviceSerial", deviceSerial);
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        HttpResponse response;
        try {
            response = HttpUtils.doPost(UrlBean.DELETEDEVICE, querys, null);
            System.out.println("==================" + response.toString());
            String s = EntityUtils.toString(response.getEntity());
            int code = JsonToObject.parseObject(s).getCode();
            if (code == 200) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            throw new MyException(ResultEnum.YS_SERVER_ERROR);
        }
    }

    /**
     * @Author: 文婧瑶
     * @Description: 删除子账户权限
     * @Date: 15:49 2018/6/1
     */
    public Boolean delAccess(String accountId, String deviceSerial) {

        String accessToken = accessTokenApi.getAccessToken();
        Map<String, String> querys = new HashMap<>();
        querys.put("accessToken", accessToken);
        querys.put("accountId", accountId);
        querys.put("deviceSerial", deviceSerial);
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        HttpResponse response;
        try {
            response = HttpUtils.doPost(UrlBean.DELETEACCESS, querys, null);
            System.out.println("==================" + response.toString());
            String s = EntityUtils.toString(response.getEntity());
            int code = JsonToObject.parseObject(s).getCode();
            if (code == 200) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            throw new MyException(ResultEnum.YS_SERVER_ERROR);
        }
    }

    /**
     * @Author: 文婧瑶
     * @Description: 修改设备
     * @Date: 16:33 2018/6/1
     */
    public Result updateDevice(String accountId, String deviceSerial, String deviceName) {

        String accessToken = accessTokenApi.getUserAccessToken(accountId);
        Map<String, String> querys = new HashMap<>();
        querys.put("accessToken", accessToken);
        querys.put("deviceSerial", deviceSerial);
        querys.put("deviceName", deviceName);
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        HttpResponse response;
        try {
            response = HttpUtils.doPost(UrlBean.UPDATEDEVICE, querys, null);
            System.out.println("==================" + response.toString());
            String s = EntityUtils.toString(response.getEntity());
            Result result = JsonToObject.parseObject(s);
            if (result.getCode() == 200) {
                result.setCode(1);
            }
            return result;
        } catch (Exception e) {
            throw new MyException(ResultEnum.YS_SERVER_ERROR);
        }
    }

    /**
     * @Author: 文婧瑶
     * @Description: 生成设备配网二维码
     * @Date: 16:35 2018/6/1
     */
    public Result qrcode(String accountId, String ssid, String password) {

        String accessToken = accessTokenApi.getUserAccessToken(accountId);
        Map<String, String> querys = new HashMap<>();
        querys.put("accessToken", accessToken);
        querys.put("ssid", ssid);
        querys.put("password", password);

        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        Map<String, String> headers = new HashMap<>();
        headers.put("Host", "open.ys7.com");
        headers.put("Content-Type", "application/x-www-form-urlencoded");

        HttpResponse response;
        try {
            response = HttpUtils.doPost(UrlBean.QRCODE, querys, null);
            System.out.println("==================" + response.toString());
            String s = EntityUtils.toString(response.getEntity());
            return JsonToObject.parseObject(s, CodeImageResponse.class);
        } catch (Exception e) {
            throw new MyException(ResultEnum.YS_SERVER_ERROR);
        }
    }

    /**
     * @Author: 文婧瑶
     * @Description: 获取摄像头列表
     * @Date: 10:47 2018/4/28
     */
    public Result list(String accountId, int pageStart, int pageSize) {

        String accessToken = accessTokenApi.getUserAccessToken(accountId);
        Map<String, String> querys = new HashMap<>();
        querys.put("accessToken", accessToken);
        querys.put("pageStart", String.valueOf(pageStart));
        querys.put("pageSize", String.valueOf(pageSize));

        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        Map<String, String> headers = new HashMap<>();
        headers.put("Host", "open.ys7.com");
        headers.put("Content-Type", "application/x-www-form-urlencoded");

        HttpResponse response;
        try {
            response = HttpUtils.doPost(UrlBean.LIST, querys, null);
            System.out.println("==================" + response.toString());
            String s = EntityUtils.toString(response.getEntity());
            Result result = JsonToObject.parseObject(s);
            if (result.getCode() == 200) {
                result.setCode(1);
            }
            return result;
        } catch (Exception e) {
            throw new MyException(ResultEnum.YS_SERVER_ERROR);
        }
    }

    /**
     * @Author: 文婧瑶
     * @Description: 获取直播地址
     * @Date: 10:58 2018/4/28
     */
    public Result getBroadcastAddress(String accountId, String source) {

        String accessToken = accessTokenApi.getUserAccessToken(accountId);
        Map<String, String> querys = new HashMap<>();
        querys.put("accessToken", accessToken);
        querys.put("source", source);

        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        Map<String, String> headers = new HashMap<>();
        headers.put("Host", "open.ys7.com");
        headers.put("Content-Type", "application/x-www-form-urlencoded");

        HttpResponse response;
        try {
            response = HttpUtils.doPost(UrlBean.GET, querys, null);
            System.out.println("==================" + response.toString());
            String s = EntityUtils.toString(response.getEntity());
            Result result = JsonToObject.parseObject(s);
            if (result.getCode() == 200) {
                result.setCode(1);
            }
            return result;
        } catch (Exception e) {
            throw new MyException(ResultEnum.YS_SERVER_ERROR);
        }
    }

    /**
     * @Author: 文婧瑶
     * @Description: 获取报警消息列表
     * @Date: 18:07 2018/4/28
     */
    public Result policeList(String accountId, Long startTime, Long endTime, Integer alarmType, Integer status, Integer pageStart, Integer pageSize) {

        String accessToken = accessTokenApi.getUserAccessToken(accountId);
        Map<String, String> querys = new HashMap<>();
        querys.put("accessToken", accessToken);
        if (startTime != null && !"".equals(startTime)) {
            querys.put("startTime", String.valueOf(startTime));
        }
        if (endTime != null && !"".equals(endTime)) {
            querys.put("endTime", String.valueOf(endTime));
        }
        if (alarmType != null) {
            querys.put("alarmType", String.valueOf(alarmType));
        }
        if (status != null) {
            querys.put("status", String.valueOf(status));
        }
        if (pageStart != null) {
            querys.put("pageStart", String.valueOf(pageStart));
        }
        if (pageSize != null) {
            querys.put("pageSize", String.valueOf(pageSize));
        }

        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        Map<String, String> headers = new HashMap<>();
        headers.put("Host", "open.ys7.com");
        headers.put("Content-Type", "application/x-www-form-urlencoded");

        HttpResponse response;
        try {
            response = HttpUtils.doPost(UrlBean.POLICELIST, querys, null);
            System.out.println("==================" + response.toString());
            String s = EntityUtils.toString(response.getEntity());
            Result result = JsonToObject.parseObject(s);
            if (result.getCode() == 200) {
                result.setCode(1);
            }
            return result;
        } catch (Exception e) {
            throw new MyException(ResultEnum.YS_SERVER_ERROR);
        }
    }

    /**
     * @Author: 文婧瑶
     * @Description: 开启设备视频加密
     * @Date: 10:17 2018/6/4
     */
    public Result videoEncryptionOn(String accountId, String deviceSerial) {

        String accessToken = accessTokenApi.getUserAccessToken(accountId);
        Map<String, String> querys = new HashMap<>();
        querys.put("accessToken", accessToken);
        querys.put("deviceSerial", deviceSerial);

        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        Map<String, String> headers = new HashMap<>();
        headers.put("Host", "open.ys7.com");
        headers.put("Content-Type", "application/x-www-form-urlencoded");

        HttpResponse response;
        try {
            response = HttpUtils.doPost(UrlBean.VIDEOENCRYPTIONON, querys, null);
            System.out.println("==================" + response.toString());
            String s = EntityUtils.toString(response.getEntity());
            Result result = JsonToObject.parseObject(s);
            if (result.getCode() == 200) {
                result.setCode(1);
            }
            return result;
        } catch (Exception e) {
            throw new MyException(ResultEnum.YS_SERVER_ERROR);
        }
    }

    /**
     * @Author: 文婧瑶
     * @Description: 关闭设备视频加密
     * @Date: 10:51 2018/6/4
     */
    public Result videoEncryptionOff(String accountId, String deviceSerial, String validateCode) {

        String accessToken = accessTokenApi.getUserAccessToken(accountId);
        Map<String, String> querys = new HashMap<>();
        querys.put("accessToken", accessToken);
        querys.put("deviceSerial", deviceSerial);
        querys.put("validateCode", validateCode);

        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        Map<String, String> headers = new HashMap<>();
        headers.put("Host", "open.ys7.com");
        headers.put("Content-Type", "application/x-www-form-urlencoded");

        HttpResponse response;
        try {
            response = HttpUtils.doPost(UrlBean.VIDEOENCRYPTIONOFF, querys, null);
            System.out.println("==================" + response.toString());
            String s = EntityUtils.toString(response.getEntity());
            Result result = JsonToObject.parseObject(s);
            if (result.getCode() == 200) {
                result.setCode(1);
            }
            return result;
        } catch (Exception e) {
            throw new MyException(ResultEnum.YS_SERVER_ERROR);
        }
    }

    /**
     * @Author: 文婧瑶
     * @Description: 设备布撤防
     * @Date: 16:14 2018/6/4
     */
    public Result setProtection(String accountId, String deviceSerial, int isDefence) {

        String accessToken = accessTokenApi.getUserAccessToken(accountId);
        Map<String, String> querys = new HashMap<>();
        querys.put("accessToken", accessToken);
        querys.put("deviceSerial", deviceSerial);
        querys.put("isDefence", String.valueOf(isDefence));

        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        Map<String, String> headers = new HashMap<>();
        headers.put("Host", "open.ys7.com");
        headers.put("Content-Type", "application/x-www-form-urlencoded");

        HttpResponse response;
        try {
            response = HttpUtils.doPost(UrlBean.SETPROTECTION, querys, null);
            System.out.println("==================" + response.toString());
            String s = EntityUtils.toString(response.getEntity());
            Result result = JsonToObject.parseObject(s);
            if (result.getCode() == 200) {
                result.setCode(1);
            }
            return result;
        } catch (Exception e) {
            throw new MyException(ResultEnum.YS_SERVER_ERROR);
        }
    }

    public Result deviceInfo(String accountId, String deviceSerial) {

        String accessToken = accessTokenApi.getUserAccessToken(accountId);
        Map<String, String> querys = new HashMap<>();
        querys.put("accessToken", accessToken);
        querys.put("deviceSerial", deviceSerial);

        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        Map<String, String> headers = new HashMap<>();
        headers.put("Host", "open.ys7.com");
        headers.put("Content-Type", "application/x-www-form-urlencoded");

        HttpResponse response;
        try {
            response = HttpUtils.doPost(UrlBean.DEVICEINFO, querys, null);
            System.out.println("==================" + response.toString());
            String s = EntityUtils.toString(response.getEntity());
            Result result = JsonToObject.parseObject(s);
            if (result.getCode() == 200) {
                result.setCode(1);
            }
            return result;
        } catch (Exception e) {
            throw new MyException(ResultEnum.YS_SERVER_ERROR);
        }
    }


}
